package com.example.demo.service.impl;

import java.util.List;

import javax.persistence.criteria.Predicate;

import org.aspectj.bridge.MessageWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;

import com.example.demo.pojo.User;
import com.example.demo.repository.UserRepository;
import com.example.demo.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserRepository userRepository;

	// 分页查询
	// findAll(Specification,Pageable):有条件的分页
	// findAll(Pageable)：没有条件的分页
	// PageRequest对象是Pageable接口的实现类
	// 两个参数 page当前页数(从0开始) size每页查询的数量
	@Override
    public Page<User> findAll(int pageNum, int pageSize) {

        Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Order.asc("id")));
        Page<User> users = userRepository.findAll(pageable);

        return users;
    }
	@Override
	public List<User> findAll() {
		// TODO Auto-generated method stub
		return userRepository.findAll();
	}

	@Override
	public User findUserById(long id) {
		// TODO Auto-generated method stub
		return userRepository.findUserById(id);
	}

	@Override
	public List<User> findUsersBylastName(String lastName) {
		// TODO Auto-generated method stub
		return userRepository.findUserBylastName(lastName);
	}

	@Override
	public void save(User user) {
		// TODO Auto-generated method stub
		userRepository.save(user);

	}

	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		 userRepository.deleteById(id);
	}

	@Override
	public void update(User user) {
		// TODO Auto-generated method stub
		userRepository.save(user);
	}

}
